﻿using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.XPath;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO.Packaging;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;

namespace Components
{
    public class createConstElements
    {
        public static int space = 0;

        public static void addTopic(ref Body body, Dictionary<string, string> topicTitleAndId, XmlNode topic)
        {
            foreach (XmlNode node in topic)
            {
                if (node.HasChildNodes)
                {
                    space++;
                    addTopic(ref body, topicTitleAndId, node);
                    space--;
                }

                if (node.NodeType == XmlNodeType.Element && node.Name == "Topic")
                {
                    string id = node.Attributes["id"].InnerText;
                    foreach (string topicId in topicTitleAndId.Keys)
                    {
                        if (id == topicId)
                        {
                            string title = topicTitleAndId[topicId];
                            int width = title.Length + space * 4;
                            string txt = title.PadLeft(width, ' ');

                            Paragraph p = new Paragraph();
                            ParagraphProperties pPr = new ParagraphProperties();
                            SpacingBetweenLines spacing = new SpacingBetweenLines() { After = "0", Before = "0" };
                            pPr.Append(spacing);
                            p.Append(pPr);
                            Run r = new Run();
                            Text text = new Text() { Space = SpaceProcessingModeValues.Preserve };
                            text.Text = txt;
                            r.Append(text);
                            p.Append(r);

                            body.Append(p);
                        }
                    }
                }
            }
        }

        public static void createContent(ref Body body, Dictionary<string, string> topicTitleAndId, string contentLayout)
        {

            body.Append(new Paragraph(
                new ParagraphProperties(
                    new Justification() { Val = JustificationValues.Center }),
                    new Run(new Text("Содержание"))));


            XmlDocument doc = new XmlDocument();
            doc.Load(contentLayout);
            if (doc.HasChildNodes)
            {
                foreach (XmlNode node in doc)
                {
                    if (node.HasChildNodes)
                    {
                        addTopic(ref body, topicTitleAndId, node);
                    }

                    if (node.NodeType == XmlNodeType.Element && node.Name == "Topic")
                    {
                        string id = node.Attributes["id"].InnerText;
                        string title = topicTitleAndId[id];

                        Paragraph p = new Paragraph();
                        ParagraphProperties pPr = new ParagraphProperties();
                        SpacingBetweenLines spacing = new SpacingBetweenLines() { After = "0", Before = "0" };
                        pPr.Append(spacing);
                        p.Append(pPr);
                        p.Append(new Run(new Text(title)));

                        body.Append(p);
                    }
                }
            }
        }
    }
}
